﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Senthil.Mobile.WhereAmI
{
    public partial class CustomOpenFileDialog : Form
    {
        public CustomOpenFileDialog()
        {
            InitializeComponent();
        }

        public string InitialDirectory { get; set; }

        string currentDir;
        string selectedFileName;

        public string SelectedFileName
        {
            get { return selectedFileName; }
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            var dir = new DirectoryInfo(currentDir);
            UpdateViewForDirectory(dir.Parent.FullName);
        }

        private void CustomOpenFileDialog_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(InitialDirectory))
                InitialDirectory = @"\";

            UpdateViewForDirectory(InitialDirectory);
        }

        private void UpdateViewForDirectory(string dir)
        {
            currentDir = dir;

            var d = new DirectoryInfo(dir);
            goUpMenuItem.Enabled = d.Parent != null;

            pathTextBox.Text = dir;

            contentsListView.Items.Clear();

            var subDirectories = new List<DirectoryInfo>(d.GetDirectories());
            subDirectories.Sort((d1, d2) => d1.Name.CompareTo(d2.Name));

            
            foreach (var subDir in subDirectories)
            {
                ListViewItem item = new ListViewItem(subDir.Name);
                item.Tag = subDir;
                contentsListView.Items.Add(item);
            }

            var files = new List<FileInfo>(d.GetFiles());
            files.Sort((f1, f2) => f1.Name.CompareTo(f2.Name));

            foreach (var file in files)
            {
                ListViewItem item = new ListViewItem(file.Name);
                item.Tag = file;
                contentsListView.Items.Add(item);
            }

            if (contentsListView.Items.Count > 0)
                contentsListView.Items[0].Selected = true;
        }

        private void CustomOpenFileDialog_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                int selectedIndices = contentsListView.SelectedIndices.Count;

                if (selectedIndices > 0)
                {
                    var item = contentsListView.Items[contentsListView.SelectedIndices[0]];

                    ProcessSelectedItem(item);
                }
            }
        }

        private void ProcessSelectedItem(ListViewItem item)
        {
            DirectoryInfo dirInfo = item.Tag as DirectoryInfo;

            if (dirInfo != null)
            {
                UpdateViewForDirectory(dirInfo.FullName);
            }
            else
            {
                FileInfo fileInfo = (FileInfo)item.Tag;
                selectedFileName = fileInfo.FullName;
                this.DialogResult = DialogResult.OK;
            }
        }

        private void cancelMenuItem_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}